﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;

using edowork.com.SQL2005;

namespace WebInvoicingsys.system
{
    public class sale_product_class
    {
        DataTable dtList;

        string connectionString;

        public decimal Total_qty { get; set; }
        public decimal Total_price { get; set; }

        private void getTotla()
        {
            Total_qty = 0;
            Total_price = 0;

            //统计 数量合计， 金额， 单价
            foreach (DataRow dr in dtList.Rows)
            {
                Total_qty = Total_qty + Decimal.Parse(dr["qty"].ToString());
                Total_price = Total_price + Decimal.Parse(dr["price"].ToString());
            }

            Total_price = Total_price * Total_qty;
        }

        public sale_product_class(string connectionString)
        {
            this.connectionString = connectionString;

            dtList = new DataTable();

            dtList.Columns.Add("productcode");
            dtList.Columns.Add("productname");
            dtList.Columns.Add("productshortname");
            dtList.Columns.Add("goodsclassname");
            dtList.Columns.Add("goodsunitname");
            dtList.Columns.Add("goodsspecname");
            dtList.Columns.Add("stackname");
            dtList.Columns.Add("guardname");
            dtList.Columns.Add("qty");
            dtList.Columns.Add("price");
            dtList.Columns.Add("remark");
        }

        public bool Add(string productcode, string qty, string in_price, string remark, ref string err_msg)
        {
            err_msg = "";

            decimal _qty, _in_price;

            if (String.IsNullOrEmpty(productcode))
            {
                err_msg = Errormsg.purchaseplan_product_IsNullOrEmpty;
                return false;
            }

            if (Decimal.TryParse(qty, out _qty) == false)
            {
                err_msg = Errormsg.purchaseplan_product_QtyFormatError;
                return false;
            }


            if (Decimal.TryParse(in_price, out _in_price) == false)
            {
                _in_price = 0;
            }

            //检查是否已存在相同的货品条码            
            if (dtList.Select(String.Format("productcode='{0}'", productcode)).Length > 0)
            {
                err_msg = Errormsg.purchaseplan_product_ExistProduct;
                return false;
            }

            //构造SQL语句
            string sql = @"select a.productcode, a.productname, a.productshortname, a.goodsclassname, a.goodsunitname, a.goodsspecname, a.stackname, a.guardname
                              from  v_product a
                                where a.productcode = '{0}'";

            sql = String.Format(sql, productcode);

            Execute exec = new Execute(connectionString);

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref err_msg);

            if((ds == null) || (ds.Tables.Count <= 0) || (ds.Tables[0].Rows.Count != 1))
            {
                err_msg = Errormsg.purchaseplan_product_NotFoundProduct;
                return false;
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                DataRow newdr = dtList.NewRow();

                newdr["productcode"] = dr["productcode"].ToString();
                newdr["productname"] = dr["productname"].ToString();
                newdr["productshortname"] = dr["productshortname"].ToString();
                newdr["goodsclassname"] = dr["goodsclassname"].ToString();
                newdr["goodsunitname"] = dr["goodsunitname"].ToString();
                newdr["goodsspecname"] = dr["goodsspecname"].ToString();
                newdr["stackname"] = dr["stackname"].ToString();
                newdr["guardname"] = dr["guardname"].ToString();
                newdr["qty"] = _qty;
                newdr["price"] = _in_price;
                newdr["remark"] = remark;

                dtList.Rows.Add(newdr);
            }

            getTotla();

            return true;
        }

        public void Reset()
        {
            dtList.Rows.Clear();

            getTotla();
        }

        public void Delect(string productcode)
        {
            DataRow[] drFind = dtList.Select(String.Format("productcode='{0}'", productcode));

            foreach (DataRow dr in drFind)
            {
                dtList.Rows.Remove(dr);
            }

            getTotla();
        }

        public DataTable getList()
        {
            return this.dtList;
        }

        public int getListTotal()
        {
            return dtList.Rows.Count;
        }

        public bool getDataList(string pcsid)
        {
            string err_msg = "";

            dtList.Rows.Clear();

            //构造SQL语句
            string sql = @"select a.productcode, a.productname, a.productshortname, b.goodsclassname, c.goodsunitname, d.goodsspecname, e.stackname, f.guardname, g.qty, g.price, g.remark
                              from  product a, goodsclass b, goodsunit c, goodsspec d, stack e, guard f, sellbilldetail g
                                where a.goodsclassid=b.goodsclassid
                                  and a.goodsunitid=c.goodsunitid
                                  and a.goodsspecid=d.goodsspecid
                                  and a.stackid=e.stackid
                                  and a.guardid=f.guardid
                                  and a.productcode = g.productcode
                                  and g.sabid='{0}'";

            sql = String.Format(sql, pcsid);

            Execute exec = new Execute(connectionString);

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref err_msg);

            if (ds == null)
            {
                return false;
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                DataRow newdr = dtList.NewRow();

                newdr["productcode"] = dr["productcode"].ToString();
                newdr["productname"] = dr["productname"].ToString();
                newdr["productshortname"] = dr["productshortname"].ToString();
                newdr["goodsclassname"] = dr["goodsclassname"].ToString();
                newdr["goodsunitname"] = dr["goodsunitname"].ToString();
                newdr["goodsspecname"] = dr["goodsspecname"].ToString();
                newdr["stackname"] = dr["stackname"].ToString();
                newdr["guardname"] = dr["guardname"].ToString();
                newdr["qty"] = dr["qty"].ToString();
                newdr["price"] = dr["price"].ToString();
                newdr["remark"] = dr["remark"].ToString();

                dtList.Rows.Add(newdr);
            }

            getTotla();

            return true;
        }
    }
}